package com.chijun.mform.create.dialog;

import android.content.Context;

import androidx.annotation.NonNull;

import com.chijun.mform.FormBuilder;
import com.chijun.mform.R;
import com.example.ccj.b.c.custom.tree.TreeMode;
import com.example.ccj.b.c.custom.tree.tree2.TreeStructure2;
import com.example.ccj.b.c.dialog.DialogBase;
import com.example.ccj.u.StringUtils;
import com.example.ccj.u.ToastUtils;
import com.example.ccj.u.server.MyObserver;

import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * 选择人员dialog
 * 点击ok后返回
 */
public class SelectPersonNewDialog extends DialogBase {

    private String treeViewName;
    private final ISelectProcessPerson iSelectProcessPerson;
    private final List<String> selectedList;
    private final boolean cnSelectBuMen;
    private TreeStructure2 treeView;
    boolean onlySelectBuMen = false;
    int  maxSum = 0;


    public SelectPersonNewDialog(@NonNull Context context,
                                 String treeViewName,
                                 List<String> selectedList,
                                 boolean cnSelectBuMen,ISelectProcessPerson iSelectProcessPerson) {
        super(context);
        this.treeViewName = treeViewName;
        this.iSelectProcessPerson = iSelectProcessPerson;
        this.selectedList = selectedList;
        this.cnSelectBuMen = cnSelectBuMen;
    }

    /**
     *
     * @param context 上下文
     * @param treeViewName 名称
     * @param selectedList 已选择列表 要传入已选择人员的id
     * @param maxSum 可选择数量
     * @param canSelectBuMen 是否可选择部门
     * @param onlySelectBuMen  是否只选择部门
     * @param iSelectProcessPerson 选择回调
     */
    public SelectPersonNewDialog(@NonNull Context context,
                                 String treeViewName,
                                 List<String> selectedList,int maxSum,
                                 boolean canSelectBuMen,boolean onlySelectBuMen,ISelectProcessPerson iSelectProcessPerson) {
        super(context);
        this.treeViewName = treeViewName;
        this.iSelectProcessPerson = iSelectProcessPerson;
        this.selectedList = selectedList;
        this.cnSelectBuMen = canSelectBuMen;
        this.onlySelectBuMen = onlySelectBuMen;
        this.maxSum = maxSum;
    }

    @Override
    public void onInit() {
        isCanClose(true);
        setLayout(R.layout.dialog_select_person_new);
        setAlpha(1f);//透明度
        isCanClose(true);//点击允许关闭
        isShowBackground(false);//不显示背景
        setShowHeightRatio(true);//按比例显示
        setHeightRatio(0.7);//高度显示比例
        showInBottom();//底部弹出
        setFull(true);//是否铺满
        goneTitleLeftImage();

        treeView = findViewById(R.id.dialog_select_person_tree2);

        if (StringUtils.isEmpty(treeViewName)) treeViewName = "请选择人员";
        setTitleCenterText(treeViewName);
        setTitleRightText("OK").setOnClickListener(V->{
            if (iSelectProcessPerson != null) iSelectProcessPerson.onOver(treeView.getSelectedData());
            dismiss();
        });


        Observable.create(new ObservableOnSubscribe<List<TreeMode>>() {
            @Override
            public void subscribe(ObservableEmitter<List<TreeMode>> e) throws Exception {
                FormBuilder.getInstance().resultTreeModeList(new IGetServerPersonResult() {
                    @Override
                    public void result(List<TreeMode> treeModeList) {
                        e.onNext(treeModeList);
                    }
                });
            }}).subscribeOn(Schedulers.newThread()).observeOn(AndroidSchedulers.mainThread())
            .subscribe(new MyObserver<List<TreeMode>>() {
                @Override
                public void onNext(List<TreeMode> treeModeList) {
                    if (treeModeList == null || treeModeList.isEmpty()) {
                        ToastUtils.showShort("未设置人员或者无相关人员，请检查！");
                        return;
                    }
                    setTreeModeList(treeModeList,selectedList,cnSelectBuMen);
                }

                @Override
                public void onError(Throwable e) {
                    super.onError(e);
                }
            });
    }

    /**
     *
     * @param treeModeList 人员目录
     * @param selectedList 已选中的数据
     * @param cnSelectBuMen 是否可选中部门
     */
    public void setTreeModeList(List<TreeMode> treeModeList,List<String> selectedList,boolean cnSelectBuMen){
        treeView.setMainText(FormBuilder.organization_name);
        treeView.setSelectMaxNum(maxSum);
        treeView.setOnlyShowMuLu(onlySelectBuMen);
        treeView.setCnSelectBuMen(cnSelectBuMen);
        treeView.setData(treeModeList,selectedList);
    }

}
